using System;
using System.Collections.Generic;
using System.Text;

namespace ParsingLogic.Types
{
    public class AllDispatcherTypes
    {
        List<BTSMessageType> _btsMessages;
        public List<BTSMessageType> BTSMessageList
        {
            get
            {
                if (_btsMessages == null)
                {
                    _btsMessages = new List<BTSMessageType>();
                }
                return _btsMessages;
            }
            set
            {
                _btsMessages = value;
            }
        }
        List<ContextLibrary> _contextLibraries;
        public List<ContextLibrary> ContextLibraryList
        {
            get
            {
                if (_contextLibraries == null)
                {
                    _contextLibraries = new List<ContextLibrary>();
                }
                return _contextLibraries;
            }
            set
            {
                _contextLibraries = value;
            }
        }

        public void Save(string FileName)
        {

            System.Xml.Serialization.XmlSerializer objXmlSer = new System.Xml.Serialization.XmlSerializer(typeof(Types.AllDispatcherTypes));
            System.Xml.XmlTextWriter _metaDataWriter = new System.Xml.XmlTextWriter(FileName, null);
            objXmlSer.Serialize(_metaDataWriter, this);
            _metaDataWriter.Flush();
            _metaDataWriter.Close();

        }

        public static AllDispatcherTypes Load(string FileName)
        {
            if (System.IO.File.Exists(FileName))
            {
                System.Xml.Serialization.XmlSerializer dispXMLSerializer = new System.Xml.Serialization.XmlSerializer(typeof(AllDispatcherTypes));
                System.Xml.XmlReader _rdr = System.Xml.XmlReader.Create(FileName);
                AllDispatcherTypes _existing = (AllDispatcherTypes)dispXMLSerializer.Deserialize(_rdr);
                _rdr.Close();
                return _existing;
            }
            else
            {
                return new AllDispatcherTypes();
            }
        }

        public bool BTSMessageTypeExists(string MessageName)
        {
            foreach (BTSMessageType msg in this.BTSMessageList)
            {
                if (msg.ClassName == MessageName)
                {
                    return true;
                }
            }
            return false;
        }
        public void OverwriteBTSMessageType(BTSMessageType Message)
        {
            int itemToRemove = 0;
            bool found = false;
            for (int i = 0; i < this.BTSMessageList.Count; i++)
            {
                if (this.BTSMessageList[i].ClassName == Message.ClassName)
                {
                    found = true;
                    itemToRemove = i;
                }

            }
            if (found)
            {
                this.BTSMessageList.RemoveAt(itemToRemove);
                this.BTSMessageList.Add(Message);
            }
        }
    }
}
